﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace jisuanqi0628
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //KeyPreview = true;
            this.Activate();
            
        }
        public double Calculate(string num1, string yunsuafu, string num2)
        {
            bool b = false;
            double ys1 = GetNumber(num1, ref b);
            double ys2 = GetNumber(num2, ref b);

            switch (yunsuafu)
            {
                case "+":
                    return ys1 + ys2;
                case "-":
                    return ys1 - ys2;
                case "*":
                    return ys1 * ys2;
                case "/":
                    return ys1 / ys2;
                default:
                    break;
            }
            return 0;
        }  
        /// <param name="num1">第一个数字</param>
        /// <param name="yunsuafu">运算符</param>
        /// <param name="num2">第二个数字</param>
        /// <returns>数据是否合法，合法返回true，不合法返回false</returns>
        public bool CheckData(string num1, string yunsuafu, string num2)
        {
            //检查输入的数据
            /*
             1.输入的字符是否是数字
             2.当运算符是/时，除数不能为0
             */
            if ("" == yunsuafu)
            {
                MessageBox.Show("请选择一个符号");
                return false;             //返回
            }
            bool b = false;
            GetNumber(num1, ref b);
            if (false == b)
            {
                MessageBox.Show("第一个参数不是合法的数字");
                return false;
            }
            GetNumber(num2, ref b);
            if (false == b)
            {
                MessageBox.Show("第二个参数不是合法的数字");
                return false;
            }

            if ("/" == yunsuafu && "0" == num2)
            {
                MessageBox.Show("除法运算时第二个参数不能是0");
                return false;
            }
            return true;  //检测通过
        }     
        /// 检查字符串是否为合法的数字      
        /// <param name="str">输入的字符串</param>
        /// <param name="b">返回值，是否合法</param>
        /// <returns>如果合法返回转换后的值</returns>
        public double GetNumber(string str, ref bool b)
        {           
            double number = 0;
            if (double.TryParse(str, out number))
            {               
                b = true;
                return number;
            }
            else
            {
                b = false;
                return 0;
            }
        }

        //错误原因两个18按钮
        //private void Button18_Click(object sender, EventArgs e)
        //{
        //    yunsuafu = "";
        //    textBox4.Text = "";
        //    num1 = "";
        //}
        //错误原因两个18
        public bool n2Cls = false;
        public void DealNumber(string arg)
        {
            if (yunsuafu == "")
            {
                DealTxt(arg);
            }
            else
            {
                if (false == n2Cls)
                {
                    textBox4.Text = "";
                    n2Cls = true;
                }
                DealTxt(arg);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="arg"></param>
        public void DealTxt(string arg)
        {
            if ("+/-" == arg)
            {
             bool b = false;
             double ys1 = GetNumber(textBox4.Text, ref b);

             textBox4.Text = ((-1) * ys1).ToString();
            }
            else if ("." == arg)
            {
                if (textBox4.Text.Contains("."))
                {
                    return;
                }
               else
                { 
                 textBox4.Text = textBox4.Text + arg;
                }
            }
             else
            {
              textBox4.Text = textBox4.Text + arg;
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            DealNumber("7");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DealNumber("8");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DealNumber("9");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DealNumber("4");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DealNumber("5");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DealNumber("6");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            DealNumber("1");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            DealNumber("2");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            DealNumber("3");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            DealNumber("+/-");
        }      

        private void button11_Click(object sender, EventArgs e)
        {
            DealNumber("0");
        }

        private void button12_Click(object sender, EventArgs e)
        {
            DealNumber(".");
        }

        private void button13_Click(object sender, EventArgs e)
        {
            DealYsf("+");
        }
        public void DealYsf(string yunsuafu1)
        {
            if ("" == textBox4.Text)
            {
                return;
            }
            num1 = textBox4.Text;
            yunsuafu = yunsuafu1;
            n2Cls = false;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            DealYsf("-");
        }

        private void button15_Click(object sender, EventArgs e)
        {
            DealYsf("*");
        }

        private void button16_Click(object sender, EventArgs e)
        {
            DealYsf("/");
        }
        public string num1;     
        public string yunsuafu = "";//yunsuafu
        private void button18_Click(object sender, EventArgs e) 
        {
            //点击等号
            //检查数据
            bool b = CheckData(num1, yunsuafu, textBox4.Text);
            if (false == b)
            {
                return;
            }
            textBox5.AppendText(Environment.NewLine + num1 + yunsuafu + textBox4.Text);           
            textBox4.Text = Calculate(num1, yunsuafu, textBox4.Text).ToString(); //执行计算
            textBox5.Text = textBox5.Text + "=" + textBox4.Text;
            n2Cls = false;
        }       
        private void button19_Click(object sender, EventArgs e)
        {
            double number = 0;
            double.TryParse(textBox4.Text, out number);
            textBox4.Text = (number * number).ToString();
            n2Cls = false;
        }

        private void button20_Click(object sender, EventArgs e)
        {
            double number = 0;
            double.TryParse(textBox4.Text, out number);
            textBox4.Text = Math.Sqrt(number).ToString();
            n2Cls = false;
        }

        private void button17_Click(object sender, EventArgs e)
        {
            yunsuafu = "";
            textBox4.Text = "";
            num1 = "";
        }

        //private void Form1_KeyDown(object sender, KeyEventArgs e)

        //{
        //    if (e.KeyCode >= Keys.D1 && e.KeyCode <= Keys.D9)
        //    {
        //        // 按下数字键1到9时，在按钮1到9之间切换焦点
        //        int buttonIndex = e.KeyCode - Keys.D0;
        //        string buttonName = "button" + buttonIndex;
        //        Control[] controls = Controls.Find(buttonName, true);
        //        if (controls.Length > 0 && controls[0] is Button)
        //        {
        //            Button button = (Button)controls[0];
        //            button.Focus();
        //        }
        //    }
        //    else if (e.KeyCode == Keys.Add)            {
        //        // 按下+键时，触发按钮Add的点击事件   +
        //        button13.PerformClick();
        //    }
        //    else if (e.KeyCode == Keys.Subtract)
        //    {
        //        // 按下-键时，触发按钮Subtract的点击事件
        //        button14.PerformClick();
        //    }
        //    else if (e.KeyCode == Keys.Multiply)
        //    {
        //        // 按下*键时，触发按钮Multiply的点击事件
        //        button15.PerformClick();
        //    }
        //    else if (e.KeyCode == Keys.Divide)
        //    {
        //        // 按下/键时，触发按钮Divide的点击事件
        //        button16.PerformClick();
        //    }
        //    else if (e.KeyCode == Keys.Enter)
        //    {
        //        // 按下Enter键时，触发按钮Enter的点击事件
        //        button18.PerformClick();
        //    }
        //}
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '1' && e.KeyChar <= '9') //数字键
            {
                int buttonId = e.KeyChar - '0'; //计算按钮编号
                Control[] controls = this.Controls.Find("button" + buttonId, true); //查找按钮控件
                if (controls.Length > 0 && controls[0] is Button)
                {
                    ((Button)controls[0]).PerformClick(); //模拟单击对应的数字按钮
                }
            }

            else if (e.KeyChar == '+')
            {
                button13.PerformClick(); //模拟单击加号按钮
            }
            else if (e.KeyChar == '-')
            {
                button14.PerformClick(); //模拟单击减号按钮
            }
            //...省略其他运算符

            else if (e.KeyChar == '*' || e.KeyChar == '/')
            {
                button15.PerformClick(); //模拟单击乘号或除号按钮
            }
            else if (e.KeyChar == '\r') //回车键
            {
                button18.PerformClick(); //模拟单击等于号按钮
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true; //启用键盘捕获
            this.KeyPress += new KeyPressEventHandler(Form1_KeyPress); //注册键盘事件处理程序
        }
    }
}
